package com.item.yxlottery.yxlottery.utils;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2016/12/17 16:52
 * @文件描述    把string中的数字金额用另外的颜色标注
 *------------------------------
 */

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RedNumberUtil {

    /* 字符串中一组连续的数字单独设置颜色 */
    public static SpannableString numberWarning(String numberText, int color) {
        int start = -1, end = -1;
        boolean startState = false;
        for (int i = 0; i < numberText.length(); i++) {
            char a = numberText.charAt(i);
            if (a >= '0' && a <= '9' && startState == false) {
                startState = true;
                start = i;
            }
            if (a >= '0' && a <= '9') {
                end = i + 1;
            }
        }
        if (start != -1 && end != -1) {
            SpannableString ss = new SpannableString(numberText);
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return ss;
        } else {
            return null;
        }
    }

    /* 字符串中一组连续的数字及之后的若干位数文字设置颜色 */
    public static SpannableString numberAndText(String numberText, int digits, int color) {
        int start = -1, end = -1;
        boolean startState = false;
        for (int i = 0; i < numberText.length(); i++) {
            char a = numberText.charAt(i);
            if (a >= '0' && a <= '9' && startState == false) {
                startState = true;
                start = i;
            }
            if (a >= '0' && a <= '9') {
                end = i + 1 + digits;
            }
        }
        if (start != -1 && end != -1) {
            SpannableString ss = new SpannableString(numberText);
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return ss;
        } else {
            return null;
        }
    }

    /* 字符串中不连续的两组数字单独设置颜色(不能超过两组) */
    public static SpannableString numberWarning(String numberText, int oneColor, int twoColor) {
        int oneStart = -1, oneEnd = -1, twoStart = -1, twoEnd = -1;
        boolean oneStartState = false, twoStartState = false;
        SpannableString ss = null;
        Matcher matcher = Pattern.compile("\\d+").matcher(numberText);
        ArrayList<String> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.group(0));
        }
        if (list.size() == 1) {
            ss = numberWarning(numberText, oneColor);
        } else if (list.size() == 2) {
            for (int i = 0; i < numberText.length(); i++) {
                char a = numberText.charAt(i);
                if (a >= '0' && a <= '9' && oneStartState == false) {
                    oneStartState = true;
                    oneStart = i;
                    oneEnd = oneStart + list.get(0).length();
                    i = i + list.get(0).length() + 1;
                    continue;
                }
                if (a >= '0' && a <= '9' && twoStartState == false) {
                    twoStartState = true;
                    twoStart = i;
                    twoEnd = twoStart + list.get(1).length();
                    continue;
                }
            }
            ss = new SpannableString(numberText);
            ss.setSpan(new ForegroundColorSpan(oneColor), oneStart, oneEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ForegroundColorSpan(twoColor), twoStart, twoEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    /*- 设置红球,白球数量显示颜色 -*/
    public static SpannableString setRedAndBlueColor(String numberText, int oneColor, CharSequence sequence, int twoColor, int threeColor) {
        int index = numberText.indexOf((String) sequence);
        SpannableString ss = new SpannableString(numberText);
        ss.setSpan(new ForegroundColorSpan(oneColor), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(twoColor), index, index + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(threeColor), index + 1, numberText.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }
}
